//
//  MyMusicViewController.m
//  ToWhere
//
//  Created by dllo on 15/11/25.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "MyMusicViewController.h"
#import "CoreDatamanager.h"
#import "MusicCollection.h"
#import "MyMusicTableViewCell.h"
#import "MusicPlayViewController.h"
#import "MusicDetailModel.h"

@interface MyMusicViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) CoreDatamanager *coreDatamanager;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSArray *resultArray;

@property (nonatomic, retain) UIAlertController *al;

@end

@implementation MyMusicViewController

- (void)dealloc
{
    [_coreDatamanager release];
    [_tableView release];
    [_resultArray release];
    [_al release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.title = @"我的音旅";
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:20 * kWidth],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 25 *kWidth, 25 *kHeight);
    [btn setImage:[UIImage imageNamed:@"Food_back"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(popAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    
    [self createView];
    
    [self initTableView];

}

- (void)viewWillAppear:(BOOL)animated{
    
    self.coreDatamanager = [CoreDatamanager shareManager];
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"MusicCollection"];
    self.resultArray = [_coreDatamanager.managedObjectContext executeFetchRequest:request error:nil];
    if (self.resultArray.count == 0) {
        self.al = [UIAlertController alertControllerWithTitle:nil message:@"空空如也,快去收藏吧!" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:self.al animated:YES completion:nil];
        [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(remove) userInfo:nil repeats:YES];
    }
    [self viewDidLoad];
}
#pragma mark init
- (void)initTableView{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 *kHeight, 375 *kWidth, 603 *kHeight) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.1];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[[UIView alloc]initWithFrame:CGRectZero] autorelease];
    [self.view addSubview:_tableView];
    [_tableView release];
}
#pragma mark 创建页面
-(void)createView
{
    UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"myback"]];
    imageview.frame = self.view.frame;
    [self.view addSubview:imageview];
    [imageview release];
    
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    effectView.frame = imageview.bounds;
    effectView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.1];
    [imageview addSubview:effectView];
}

#pragma mark Action
- (void)popAction{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)remove{
    [self.al dismissViewControllerAnimated:YES completion:nil];
    
}
#pragma mark UITableView代理回调方法
#pragma mark 编辑状态
-(UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewCellEditingStyleDelete;
}
#pragma mark 完成编辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.coreDatamanager.managedObjectContext deleteObject:self.resultArray[indexPath.row]];
    
    [_coreDatamanager saveContext];
    [self viewWillAppear:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.resultArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyMusicTableViewCell *cell = [MyMusicTableViewCell MyMusicTableViewCellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [[UIColor colorWithWhite:0.851 alpha:1.000] colorWithAlphaComponent:0.5];
    cell.music = self.resultArray[indexPath.row];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 180 * kHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MusicPlayViewController *music = [MusicPlayViewController shareMusicPlayViewController];
    self.hidesBottomBarWhenPushed = YES;
    music.row = indexPath.row;
    NSMutableArray *arrayOld = [NSMutableArray array];
    for (MusicCollection *music in self.resultArray) {
    
        MusicDetailModel *model = [[MusicDetailModel alloc]init];
        model.source = [music source];
        model.ID = [music musicID];
        model.pic_750 = [music pic_750];
        model.name = [music name];
        model.User = @{@"name":[music user]};
        [arrayOld addObject:model];
    }
    
    NSArray *array = [NSArray arrayWithArray:arrayOld];
    music.playListArray = array;
    music.backImage = [self.resultArray[indexPath.row] pic_750];
    music.collec = 1;
    [self.navigationController pushViewController:music animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
